package com.douqu.game.core.config;

import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;

/**
* Created by ${ZhaoLong} on 2018/3/28.
*/
public class AdConfig extends GameObject {
    public String icon;

    public int openCondition;

    public GoodsData[] durationTime;

    public String skip;

    public int weight;



    @Override
    public boolean check() {
        if (durationTime == null) {
            LogUtils.error("AdConfig check durationTime is null -> id:" + id + " name:" + name);
            return false;
        }

        return true;
    }

    @Override
    public boolean setVariable(String key, String value) {
         if ("durationTime".equals(key)) {
             durationTime = LoadUtils.loadGoodDataToArray(key, value);
             if(durationTime.length == 0){
                 durationTime = new GoodsData[]{new GoodsData()};
             }
             for(GoodsData data : durationTime){
                 if(data.type != 0){
                     data.type = (int)(TimeUtils.getDateByString(data.type + "", "yyyyMMdd").getTime() / 1000);
                 }
                 if(data.id != 0){
                     data.id = (int)(TimeUtils.getDateByString(data.id + " 23:59:59", "yyyyMMdd HH:mm:ss").getTime() / 1000);
                 }else {
                     data.id = Integer.MAX_VALUE;
                 }

             }
            return true;
        } else {
            return super.setVariable(key, value);
        }
    }


    public boolean isTimeIn(){
        int currentTime = (int)(DataFactory.currentTime / 1000);
        for(GoodsData data : durationTime){
            if(currentTime > data.type &&  currentTime < data.id){
                return true;
            }
        }
        return false;
    }


}
